package com.hdax.gulimall.order.listener;


import com.hdax.gulimall.order.entity.OrderEntity;
import com.hdax.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


/**
 * 订单关闭监听器
 */
@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {


    @Autowired
    private OrderService orderService;

    /**
     * 监听队列
     */
    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            //关闭订单
            System.out.println("进入订单监听器");
            orderService.closeOrder(orderEntity);
            //丢弃消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            //消息从新回到队列不能丢弃
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }





}
